package equation;

import java.util.ArrayList;
import java.util.Random;

public class Exercise {
    private int count = 50;
    private ArrayList<Equation> equationList;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public ArrayList<Equation> getEquationList() {
        return equationList;
    }

    public void setEquationList(ArrayList<Equation> equationList) {
        this.equationList = equationList;
    }

    public Exercise() {
        equationList = new ArrayList<>();
    }

    public Exercise(int count) {
        this.count = count;
        equationList = new ArrayList<>();
    }

    //判断算式是否在习题集中已包含
    public boolean isExist(Equation paramEquation) {
        return equationList.contains(paramEquation);
    }

    //产生纯加法习题集方法
    public void generateAddExercise() {
        int i = 0;
        while (i < count) {
            Equation equation = new AddEquation();
            if (isExist(equation)) {
                continue;
            } else {
                equationList.add(equation);
                i++;
            }
        }
    }

    //产生纯减法习题集方法
    public void generateSubExercise() {
        int i = 0;
        while (i < count) {
            Equation equation = new SubEquation();
            if (isExist(equation)) {
                continue;
            } else {
                equationList.add(equation);
                i++;
            }
        }
    }

    //产生随机混合算式
    public Equation generateRandomEquation() {
        Equation equation;
        Random random = new Random();
        if (random.nextInt(2) == 0) {
            equation =new AddEquation();
        } else {
            equation = new SubEquation();
        }
        return equation;
    }

    public void generateMixExercise() {
        int i = 0;
        while (i < count) {
            Equation equation = generateRandomEquation();
            if (isExist(equation)) {
                continue;
            } else {
                equationList.add(equation);
                i++;
            }
        }
    }

    public void printExercise() {
        System.out.println("==========");
        System.out.println("小学算法试卷自动生成器V1.3 ");
        System.out.println("==========");
        for (int i = 0; i < equationList.size(); i++) {
            System.out.println("第" + (i + 1) + "题:" + equationList.get(i).toString());
        }
    }

    //打印习题集参考答案
    public void printExerciseResult() {
        System.out.println("==========");
        System.out.println("本套试卷参考答案");
        System.out.println("==========");
        for (int i = 0; i < equationList.size(); i++) {
            System.out.println("第" + (i + 1) + "题:" + equationList.get(i).getResult());
        }
    }
}


